package com.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dao.CustomerServerDao;
import com.model.Customerserver;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 客服管理相关操作类
 *  
 * @author 0912652
 */

public class SupervisorAction extends ActionSupport implements ModelDriven<Customerserver> {

	private Customerserver custServer = new Customerserver();
	
	private String sauthority;
	
	public String getSauthority(){
		return sauthority;
	}
	
	public void setSauthority(String sauthority){
		this.sauthority = sauthority;
	}
	
	// 用 java.io,File 类型 ，来定义上传文件的控件
	private File photo;
	// 拦截器自动赋值
	private String photoFileName;

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public Customerserver getModel() {
		return custServer;
	}

	
	public String addCustServer() throws IOException{
		// 获取图片路径并存入cat
		String serverRealPath = ServletActionContext.getServletContext()
				.getRealPath("/img") + "\\" + photoFileName;
		File imageFile = new File(serverRealPath);
		// 保存到数据库的相对路径
		String dateBasepath = ServletActionContext.getRequest()
				.getContextPath() + "/img/" + photoFileName;
		// 上载到服务器端！ -> apache commons-io.jar
//		FileUtils.copyFile(photo, imageFile);
		custServer.setPhoto(dateBasepath);
		if (sauthority.equals("普通客服")) {
			custServer.setAuthority(0);
		}else if (sauthority.equals("客户经理")) {
			custServer.setAuthority(1);
		}else if (sauthority.equals("管理员")) {
			custServer.setAuthority(2);
		}else {
			return "Add";
		}
		// 自动获取系统时间存入数据库
		Date date = new Date();
		java.sql.Date nousedate = new java.sql.Date(date.getTime());
		custServer.setSdate(nousedate);
		
		CustomerServerDao dao = new CustomerServerDao();
		dao.insert(custServer);
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		List<Customerserver> customerservers = dao.findAll();
		session.setAttribute("allCustServer", customerservers);
		
		return "Supervisor";
		
	}
	
	public String getCustServerById(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int sid=Integer.parseInt(request.getParameter("sid"));
		
		CustomerServerDao dao = new CustomerServerDao();
		Customerserver cs = dao.findById(sid);
		
		HttpSession session = request.getSession();
		session.setAttribute("CustServer", cs);
		
		return "Show";
	}
	
	public String removeCustServerById(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int sid=Integer.parseInt(request.getParameter("sid"));
		
		return "Supervisor";
	}
	
	public String updateCustServer(){
		return "Supervisor";
	}

}
